package com.ruoyi.api.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.api.dto.park.resp.ParkStatisticsRespDto;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.tools.utils.DateUtils;
import com.ruoyi.standingbook.park.domain.Park;
import com.ruoyi.standingbook.park.domain.vo.ParkVo;
import com.ruoyi.standingbook.park.mapper.ParkMapper;

import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Service
public class ParkStatService {
    private final ParkMapper baseMapper;
    
	/**
	 * 查询公园管养
	 * 
	 * @param date 管养时间
	 * @param pageQuery 页码信息
	 * 
	 * @return
	 */
    public TableDataInfo<ParkStatisticsRespDto> queryParkMaintenanceList(String date, PageQuery pageQuery) {
		if (StrUtil.isBlank(date)) {
			date = DateUtils.getCurrDate();
		}
		
        LambdaQueryWrapper<Park> lqw = Wrappers.lambdaQuery();
        lqw.eq(Park::getDate, date);
        
        Page<ParkVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw, ParkVo.class);
        
        return TableDataInfo.build(fromParkVoToParkStat(result));
    }
    
    
    private Page<ParkStatisticsRespDto> fromParkVoToParkStat(Page<ParkVo> parkVoList) {
    	Page<ParkStatisticsRespDto> page = new Page<>(); 
    	
    	page.setPages(page.getPages());
    	page.setTotal(page.getTotal());
    	
    	List<ParkStatisticsRespDto> parkStatList = new ArrayList<>();
    	
    	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    	
    	parkVoList.getRecords().forEach(vo->{
    		ParkStatisticsRespDto parkStatRespDto = new ParkStatisticsRespDto();
    		
    		BeanUtils.copyProperties(vo, parkStatRespDto, "serviceType", "date", "pic1");
    		
    		parkStatRespDto.setServiceType(vo.getServiceTypeName());
    		
    		parkStatRespDto.setDate(formatter.format(vo.getDate()));
    		
    		parkStatRespDto.setPic1Content(vo.getPic1());
    		
    		parkStatList.add(parkStatRespDto);
    	});
    	
    	page.setRecords(parkStatList);
    	
    	return page;
    }
}
